#!/usr/bin/env python
#
# MACHINA
# by pdp
# GNUCITIZEN (http://www.gnucitizen.org)
#

#
# STANDARD IMPORTS
#
import os
import sys
import logging
import optparse

#
# MACHINA IMPORTS
#
import machina

#
# SET PROG
#
prog = os.path.basename(sys.argv[0])

#
# PARSE COMMAND LINE OPTIONS
#
parser = optparse.OptionParser(prog=prog, version=machina.machina.__version__)
parser.add_option('-m', '--machina', action='store', type='string', dest='machina', metavar='FILE', help='specify machina')
parser.add_option('-d', '--directory', action='store', type='string', dest='directory', metavar='DIR', help='specify working directory')
parser.add_option('-D', '--define', action='append', type='string', dest='define', metavar='PAIR', help='define variable')

(options, args) = parser.parse_args()

#
# PRINT ERROR AND EXIT
#
def print_error_and_exit(msg):
	sys.stderr.write('%s: error: %s\n' % (prog, msg))
	sys.exit(1)

#
# SET THE WORKING DIRECTORY
#
if options.directory:
	try:
		os.chdir(options.directory)
		sys.path.insert(0, options.directory)
	except Exception, e:
		print_error_and_exit("directory '%s' not found" % options.directory.replace('\\', '\\\\').replace("'", r"\'"))
elif options.machina:
	dir = os.path.dirname(options.machina)

	if dir:
		os.chdir(dir)
		sys.path.insert(0, dir)
	else:
		sys.path.insert(0, '.')
else:
	sys.path.insert(0, '.')

#
# DEFINE VARIABLES
#
variables = {}
if options.define:
	for pair in options.define:
		tokens = str(pair).split('=', 1)

		variables[tokens[0].strip()] = tokens[1]

#
# SET LOGGER
#
machina.register_machina_logger()

#
# LOAD AND EXECUTE MACHINA FILE
#
def load_and_execute_machina_file(file_path):
	globals = variables
	globals.update({'__builtins__':__builtins__, 'task':machina.task})

	if not os.path.exists(file_path):
		raise Exception("machina file '%s' not found" % file_path.replace('\\', '\\\\').replace("'", r"\'"))

	execfile(file_path, globals)

	machina.task.launch_tasks()

#
# SET MACHINA
#
if options.machina:
	try:
		load_and_execute_machina_file(options.machina)
		sys.exit(0)
	except Exception, e:
		print_error_and_exit(str(e))
else:
	for file_path in ['Machinafile', 'Machinafile.py']:
		if os.path.isfile(file_path):
			try:
				load_and_execute_machina_file(file_path)
				sys.exit(0)
			except Exception, e:
				print_error_and_exit(str(e))

#
# OR ELSE...
#
print_error_and_exit("no 'Machinafile' or 'Machinafile.py' found")
